###爬取boss直聘数据
from selenium import webdriver
import time
import csv

#打开文件
f = open('./csv/boss直聘python相关工作.csv',mode = 'a',encoding = 'utf-8-sig',newline = '')
#写入文档头
csv_writer = csv.DictWriter(f,fieldnames=[
    "工作名称","工作地点","薪资","经验要求","学历要求","公司名称","公司规模","联系人"
])
csv_writer.writeheader()

#打开Chrome
driver = webdriver.Chrome()
url = 'https://www.zhipin.com/job_detail/?query=python&city=100010000&industry=&position='
#打开网站
driver.get(url)
#等待网页初加载
time.sleep(25)

#获取信息的函数
def get_info():
    lis = driver.find_elements_by_css_selector('.job-card-wrapper ')
    for li in lis:
        #工作名称
        job_name = li.find_element_by_css_selector('.job-card-left .job-title .job-name ').text
        #工作地点
        job_area = li.find_element_by_css_selector('.job-card-left .job-title .job-area-wrapper .job-area').text.replace('·',' ')
        #薪资
        job_salary = li.find_element_by_css_selector('.job-card-left .salary').text
        #经验要求
        job_experience = li.find_element_by_css_selector('.job-card-left .tag-list li:nth-child(1)').text
        #学历要求
        job_qualification = li.find_element_by_css_selector(' .job-card-left .tag-list li:nth-child(2)').text
        #公司名称
        company_name = li.find_element_by_css_selector('.job-card-right .company-name a').text
        #公司规模
        company_scale = li.find_element_by_css_selector('.job-card-right .company-tag-list li:last-child').text
        #联系人
        contact = li.find_element_by_css_selector('.job-card-left .info-public').text[0:3]
        #写入行
        dic = {
            "工作名称" : job_name,
            "工作地点" : job_area,
            "薪资" : job_salary,
            "经验要求" : job_experience,
            "学历要求" : job_qualification,
            "公司名称" : company_name,
            "公司规模" : company_scale,
            "联系人" : contact
        }
        csv_writer.writerow(dic)
        #打印数据
        print(job_name,job_area,job_salary,job_experience,job_qualification)
        print(company_name,company_scale,contact)

num = 1
#打印前10页的内容
for page in range(1,11):
    print(f"正在打印第{num}页的内容")
    num += 1
    get_info()
    #点击“下一页”的按钮
    driver.find_element_by_css_selector('.ui-icon-arrow-right').click()
    time.sleep(25)
#关闭文件
f.close()